package com.bscai.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;

/*
 *create by lmx  2021/7/16:4:13 PM
 */
public class MessageDialog extends Dialog implements View.OnClickListener{


    private TextView titleText;
    private TextView messageText;
    Button sureBtn;
    Button cancelBtn;

    String message;
    String sureTitle;
    String cancelTitle;
    String title;

    private MessageDialogCallBack dialogCallBack;

    public interface MessageDialogCallBack{
        void onSure();
        void onCancel();
    }


    public MessageDialog(@NonNull Context context) {
        super(context);
    }

    public MessageDialog(@NonNull Context context,String title, String sureText, String cancelText) {
        super(context);
        this.sureTitle = sureText;
        this.cancelTitle = cancelText;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.message_dialog_layout);
        initView();
    }

    public void setDialogCallBack(MessageDialogCallBack dialogCallBack) {
        this.dialogCallBack = dialogCallBack;
    }

    public void setMessage(String message) {
        this.message = message;
        messageText.setText(message);
    }

    public void setSureTitle(String sureTitle) {
        this.sureTitle = sureTitle;
        sureBtn.setText(sureTitle);
    }

    public void setCancelTitle(String cancelTitle) {
        this.cancelTitle = cancelTitle;
        cancelBtn.setText(cancelTitle);
    }

    public Button getCancelBtn() {
        return cancelBtn;
    }

    public Button getSureBtn() {
        return sureBtn;
    }

    void initView(){
        titleText = findViewById(R.id.title);
        messageText = findViewById(R.id.message_text);
        sureBtn = findViewById(R.id.sure_btn);
        cancelBtn = findViewById(R.id.cancel_btn);

        sureBtn.setOnClickListener(this);
        cancelBtn.setOnClickListener(this);

        if (!TextUtils.isEmpty(sureTitle)){
            sureBtn.setText(sureTitle);
        }
        if (!TextUtils.isEmpty(cancelTitle)){
            cancelBtn.setText(cancelTitle);
        }
    }



    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.sure_btn) {
            if (dialogCallBack != null) {
                dialogCallBack.onSure();
            }
        } else if (id == R.id.cancel_btn) {
            if (dialogCallBack != null) {
                dialogCallBack.onCancel();
            }
        }
    }
}
